package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.customer.mapper.ServerBankMapper;
import com.jzo2o.customer.model.domain.BankAccountUpsert;
import com.jzo2o.customer.model.dto.response.BankAccountResDTO;
import com.jzo2o.customer.service.IServerBankService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.stereotype.Service;

@Service
public class ServerBankServiceImpl extends ServiceImpl<ServerBankMapper, BankAccountUpsert> implements IServerBankService {
    @Override
    public BankAccountResDTO currentUserInfo() {


        try {
           Long  id = UserContext.currentUserId();
            BankAccountUpsert bankAccountUpsertReqDTO = this.getById(id);
            if (ObjectUtil.isEmpty(bankAccountUpsertReqDTO)){
                return null;
            }
            return BeanUtil.copyProperties(this.getById(id), BankAccountResDTO.class);

        }catch (RuntimeException r){
            throw new RuntimeException("非法操作");
        }
    }

    @Override
    public void updateAccount(BankAccountUpsert dto) {
        BankAccountUpsert one = this.lambdaQuery()
                .eq(BankAccountUpsert::getAccount, dto.getAccount())
                .one();
        if (ObjectUtil.isNotEmpty(one)){
            LambdaUpdateWrapper<BankAccountUpsert> up=new LambdaUpdateWrapper<>();
            up.eq(BankAccountUpsert::getId, one.getId());
            this.update(dto,up);
        }else {
            this.save(dto);
        }

    }
}
